package com.huihui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.base.BaseActivity;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.utils.StringUtils;
import com.huihui.widget.AddPicLayout;
import com.huihui.widget.OnPreviewListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPagerActivity;
import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;
import okhttp3.Call;


/**
 * 意见反馈
 */
public class FeedBackActivity extends BaseActivity  implements OnPreviewListener {

    private EditText edit_text;
    private EditText remain_mail;
    private TextView confirm;
    ArrayList<String> selectedPhotos = new ArrayList<>();
    private AddPicLayout addPicLayout;
    public final static int REQUEST_CODE = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
//        getSupportActionBar().setTitle(R.string.feedback);
        titleView.setText(R.string.feedback);
        initView();
        initListener();
    }

    private void initListener() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                doFeedBack();
            }
        });
    }

    private void doFeedBack() {
        if(TextUtils.isEmpty(edit_text.getText().toString()) || TextUtils.isEmpty(remain_mail.getText().toString())){
            Toast.makeText(this,"意见或联系方式不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!StringUtils.isPhone(remain_mail.getText().toString())){
            Toast.makeText(this,"请输入有效联系方式",Toast.LENGTH_SHORT).show();
            return;
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i < selectedPhotos.size();i++){
            if(!selectedPhotos.get(i).equals("000000")) {
                String str = "feedback/";
                if (i == 0) {
                    String name = getUUID32();
                    upload(str + name + ".jpg",selectedPhotos.get(i));
                    str = str + name + ".jpg";
                    sb.append(str);
                } else {
                    String name = getUUID32();
                    upload(str + name + ".jpg",selectedPhotos.get(i));
                    str = ";" + str +  name + ".jpg";
                    sb.append(str);
                }
            }
        }
        OkHttpRequest.getInstance().uploadFeedback(HttpContants.uploadFeedback, getUserInfo().getUser_id(), edit_text.getText().toString(), remain_mail.getText().toString(),sb.toString(), new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
            }

            @Override
            public void onResponse(String response) {
                //反馈成功
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if(jsonObject != null && jsonObject.has(HttpContants.STATE) && jsonObject.getInt(HttpContants.STATE) == HttpContants.SUCCESS_CODE){
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void upload(String key,String url) {
        uploadFile(key, url);
    }


    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        List<String> photos = null;
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
            }
            selectedPhotos.clear();

            if (photos != null) {
                selectedPhotos.addAll(photos);
            }
            addPicLayout.setPaths(selectedPhotos);
        }
    }


    @Override
    public void onPreview(int pos,boolean delete) {

        Intent intent = new Intent(this, PhotoPagerActivity.class);
        intent.putExtra(PhotoPagerActivity.EXTRA_CURRENT_ITEM, pos);
        intent.putExtra(PhotoPagerActivity.EXTRA_SHOW_DELETE,delete);
        if (delete){
            intent.putExtra(PhotoPagerActivity.EXTRA_PHOTOS, selectedPhotos);
        }
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void onPick() {
        PhotoPickerIntent intent = new PhotoPickerIntent(FeedBackActivity.this);
        intent.setPhotoCount(9);
        intent.setShowCamera(true);
        intent.setShowGif(false);
        startActivityForResult(intent, REQUEST_CODE);
    }


    private void initView() {
        edit_text =  findViewById(R.id.edit_text);
        remain_mail = findViewById(R.id.remain_mail);
        confirm =  findViewById(R.id.confirm);
        addPicLayout = findViewById(R.id.addPicLayout);
        addPicLayout.setOnPreviewListener(this);
    }


}
